package com.bjesc_b2c.app.appraiset.widget.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.utils.TimeListUilts;
import com.bjesc_b2c.app.appraiset.widget.BaseDialog;

import java.util.Calendar;
import java.util.List;

import cn.qqtheme.framework.widget.WheelView;


/**
 * Created by Jason Chen on 2017/2/23.
 */

public class SelectTimeDialog extends BaseDialog implements View.OnClickListener {
    private WheelView yearWv;
    private WheelView monthWv;
    private WheelView dayWv;
    private List<String> yearList;
    private List<String> cureentMonthList;
    private String year;
    private String month;
    private String day;
    private TextView cancelTv, sureTv;
    private OnSelectTimeListener timeListener;
    private int currentMonth;
    private int currentYear;
    private Calendar calendar;
    private static List<String> FebruaryLongDayArray;
    private static List<String> FebruaryShrotDayArray;
    private static List<String> LongDayArray;
    private static List<String> ShortDayArray;
    private static List<String> monthList;
    private List<String> currentDayList;
    private List<String> dayArray;
    private List<String> monthArray;
    static {
        FebruaryLongDayArray = TimeListUilts.getFebruaryLongDay();
        FebruaryShrotDayArray = TimeListUilts.getFebruaryShortDay();
        LongDayArray = TimeListUilts.getLongDayList();
        ShortDayArray = TimeListUilts.getShortDayList();
        monthList = TimeListUilts.getMonthList();
    }
    public SelectTimeDialog(Context context, OnSelectTimeListener timeListener,boolean isAftertime) {
        super(context);
        this.timeListener = timeListener;
        if(isAftertime){    //是否以后的时间
            this.yearList = TimeListUilts.getAftertimeYearList();
            this.cureentMonthList = TimeListUilts.getAftertimeMonthList();
            this.currentDayList = TimeListUilts.getAftertimeDayList();
        }else{  //是否以前的时间
            this.yearList = TimeListUilts.getBeforeYearList();
            this.cureentMonthList = TimeListUilts.getBeforeMonthList();
//            this.currentDayList = TimeListUilts.getBeforeDayList();
            this.currentDayList = TimeListUilts.getAftertimeDayList();
        }
        monthArray = cureentMonthList;
        dayArray = currentDayList;
        initViews();
        calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
    }

    private void initViews() {
        view = inflater.inflate(R.layout.dialog_select_time, null);
        yearWv = (WheelView) view.findViewById(R.id.wv_year);
        monthWv = (WheelView) view.findViewById(R.id.wv_month);
        dayWv = (WheelView) view.findViewById(R.id.wv_day);
        cancelTv = (TextView) view.findViewById(R.id.tv_cancel);
        sureTv = (TextView) view.findViewById(R.id.tv_sure);
        yearWv.setItems(yearList);
        monthWv.setItems(cureentMonthList);
        dayWv.setItems(currentDayList,"22日");
        yearWv.setOnWheelListener(yearListener);
        monthWv.setOnWheelListener(monthListener);
        dayWv.setOnWheelListener(dayListener);
        setContentView(view);
        cancelTv.setOnClickListener(this);
        sureTv.setOnClickListener(this);
        show();
    }

    private WheelView.OnWheelListener yearListener = new WheelView.OnWheelListener() {
        @Override
        public void onSelected(boolean isUserScroll, int index, String item) {
            year = yearList.get(index);
            int indexOf = year.indexOf("年");
            year = year.substring(0, indexOf);
            currentYear = Integer.parseInt(year);
//            isLeapYear();
            if(currentYear == calendar.get(Calendar.YEAR)){
                monthArray = cureentMonthList;
                monthWv.setItems(monthArray);
            }else{
                monthArray = monthList;
                monthWv.setItems(monthArray);
            }
        }
    };

    private WheelView.OnWheelListener monthListener = new WheelView.OnWheelListener() {
        @Override
        public void onSelected(boolean isUserScroll, int index, String item) {
            month = monthArray.get(index);
            int indexOf = month.indexOf("月");
            month = month.substring(0, indexOf);
            currentMonth = Integer.parseInt(month);
            if (Integer.parseInt(month) < 10) {
                month = "0" + month;
            }

            if( currentMonth == calendar.get(Calendar.MONTH) + 1){
                dayArray = currentDayList;
                dayWv.setItems(currentDayList);
            }else{
                if (currentMonth == 2) {
                    if (((currentYear) % 4 == 0 && (currentYear) % 100 != 0) || (currentYear) % 400 == 0) {
                        dayArray = FebruaryLongDayArray;
                        dayWv.setItems(FebruaryLongDayArray);
                    } else {
                        dayArray = FebruaryShrotDayArray;
                        dayWv.setItems(FebruaryShrotDayArray);
                    }
                } else {
                    if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7 || currentMonth == 8 || currentMonth == 10 || currentMonth == 12) {
                        dayArray = LongDayArray;
                        dayWv.setItems(LongDayArray);
                    } else {
                        dayArray = ShortDayArray;
                        dayWv.setItems(ShortDayArray);
                    }
                }
            }
        }
    };


    private WheelView.OnWheelListener dayListener = new WheelView.OnWheelListener() {
        @Override
        public void onSelected(boolean isUserScroll, int index, String item) {

            day = dayArray.get(index);
            if (TextUtils.isEmpty(day)) {
                return;
            }
            int indexOf = day.indexOf("日");
            if(indexOf == -1){
                return;
            }
            day = day.substring(0, indexOf);
            if (Integer.parseInt(day) < 10) {
                day = "0" + day;
            }
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_sure:
                if (timeListener != null) {
                    timeListener.onSelectTime(year, month, day);
                    dismiss();
                }
                break;
        }
    }

    public interface OnSelectTimeListener {
        void onSelectTime(String year, String month, String day);
    }
    private void isLeapYear(){
        if (currentYear == calendar.get(Calendar.YEAR) && currentMonth == calendar.get(Calendar.MONTH) + 1) {
            dayArray = currentDayList;
            dayWv.setItems(currentDayList);

        } else {
            if (currentMonth == 2) {
                if (((currentYear) % 4 == 0 && (currentYear) % 100 != 0) || (currentYear) % 400 == 0) {
                    dayArray = FebruaryLongDayArray;
                    dayWv.setItems(FebruaryLongDayArray);
                } else {
                    dayArray = FebruaryShrotDayArray;
                    dayWv.setItems(FebruaryShrotDayArray);
                }
            } else {
                if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7 || currentMonth == 8 || currentMonth == 10 || currentMonth == 12) {
                    dayArray = LongDayArray;
                    dayWv.setItems(LongDayArray);
                } else {
                    dayArray = ShortDayArray;
                    dayWv.setItems(ShortDayArray);
                }
            }

        }
    }
}
